<?php
	// 从数据库中获取数据，为了节省内存，不要把数据一次性读到内存，从句柄中一行一行读即可
	//172.31.3.21
	//127.0.0.1
	$pdo =new pdo("mysql:host=127.0.0.1;port=3306;dbname=tddb;","root","tdled2018");
	$pdo->query("set character set 'utf8'");//读库 
	$pdo->query("set names 'utf8'");//写库 
	//$sql SQL语句
	$sql = "
	(
		SELECT
			 YCID AS 异常ID
			,YiChangJiTai AS 异常机台
			,YiChangMa AS 异常码
			,CASE WHEN 1 THEN '开始时间' END AS 开始结束
			,YiChangStartTime AS 时间
			,CASE WHEN 1 THEN '无' END AS 处理代码
		FROM
			scanyichang
		-- WHERE
			-- DATE(YiChangStartTime) >= CURDATE()-1
	)
		UNION ALL
	(
		SELECT
			 YCID AS 异常ID
			,YiChangJiTai AS 异常机台
			,YiChangMa AS 异常码
			,CASE WHEN YiChangEndTime = '0000-00-00 00:00:00' THEN '当前状态' ELSE '结束时间' END AS 开始结束
			,YiChangEndTime AS 时间
			,CASE WHEN 1 THEN '无' END AS 处理代码
		FROM
			scanyichang
		-- WHERE
			-- DATE(YiChangEndTime) >= CURDATE()-1
	)
		UNION ALL
	(
		SELECT
			 id
			,DQJT AS 异常机台
			,except_number AS 异常码
			,CASE WHEN 1 THEN '开始时间' END AS 开始结束
			,startime AS 时间
			,fix_code AS 处理代码
		FROM
			fix
	)
		UNION ALL
	(
		SELECT
			 id
			,DQJT AS 异常机台
			,except_number AS 异常码
			,CASE WHEN 1 THEN '结束时间' END AS 开始结束
			,endtime AS 时间
			,fix_code AS 处理代码
		FROM
			fix
	)
	UNION ALL
	(
		SELECT
			 XJID AS 异常ID
			,YiChangJiTai AS 异常机台
			,YiChangMa AS 异常码
			,CASE WHEN 1 THEN '开始时间' END AS 开始结束
			,YiChangStartTime AS 时间
			,CASE WHEN 1 THEN '无' END AS 处理代码
		FROM
			qcxunjian
		-- WHERE
			-- DATE(YiChangStartTime) >= CURDATE()-1
	)
		UNION ALL
	(
		SELECT
			 XJID AS 异常ID
			,YiChangJiTai AS 异常机台
			,YiChangMa AS 异常码
			,CASE WHEN YiChangEndTime = '0000-00-00 00:00:00' THEN '当前状态' ELSE '结束时间' END AS 开始结束
			,YiChangEndTime AS 时间
			,CASE WHEN 1 THEN '无' END AS 处理代码
		FROM
			qcxunjian
		-- WHERE
			-- DATE(YiChangEndTime) >= CURDATE()-1
	)
		ORDER BY
			5 DESC
	"
	;
	$res=$pdo->prepare($sql);//准备查询语句
	$res->execute(); //执行一条查询语句
	$EXC=$res->fetchAll();//取出所有行
	echo json_encode(array('EXC' => $EXC));//转码成json
?>